package com.self.serviceedu.controller;


import com.self.commonutils.R;
import com.self.serviceedu.entity.EduCourse;
import com.self.serviceedu.entity.vo.CourseInfoVo;
import com.self.serviceedu.entity.vo.CoursePublishVo;
import com.self.serviceedu.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaozeng
 * @since 2020-09-16
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //课程列表显示 基本实现
    // TODO 完善条件查询带分页
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }


    //添加课程的基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //将添加成功后的课程ID做个返回，以便后面做添加课程大纲
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程ID查询课程基本信息
    @ApiOperation(value = "根据课程ID查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId ) {
        System.out.println(courseId);
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        System.out.println(courseInfoVo);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //课程基本信息的修改
    @ApiOperation(value = "课程基本信息的修改")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程ID查询课程确认信息
    @ApiOperation(value = "根据课程ID查询课程确认信息")
    @GetMapping("getPublishCourseId/{courseId}")
    public R getPublishCourseInfo(
            @ApiParam(name = "courseId",value = "课程ID",required = true)
            @PathVariable("courseId") String courseId){

        CoursePublishVo coursePublishVo = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布，修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable("id") String id){

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程的状态，Normal表示已经发布
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @ApiOperation(value = "根据课程Id删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(
            @ApiParam(name = "courseId",value = "课程Id",required = true)
            @PathVariable("courseId") String courseId){

        courseService.removeCourseById(courseId);
        return R.ok();

    }
}

